<?php


function getRuleLevel($id,$s_rules){
    $row = $s_rules[$id];
    return $row['pid'] > 0 ? getRuleLevel($row['pid'],$s_rules) : $id;
}
$current_href = strtolower(request()->controller()."/".request()->action());
$db_href = db("auth_rule")->where(array('href'=>$current_href))->find();
$ruleTrees = model('auth_rule')->genRulesTree();
$rule_pid = getRuleLevel($db_href['id'],model('auth_rule')->rules());
$rule_pid = intval($rule_pid) <= 0 ? 1 : $rule_pid;
$leftNav = array();
foreach($ruleTrees as $r){
    if($r['id'] == $rule_pid){
        if(!checkPermissions($rule_pid)){
            continue;
        }
        $leftNav = array(
            "name" => $r['title'],
            "icon" => "ace-icon fa fa-cloud",
        );
        if(isset($r['son'])){
            foreach($r['son'] as $i=>$n){
                if(!checkPermissions($n['id'])){
                    continue;
                }
                $ns[$i] = array(
                    "name" => $n['title'],
                    "icon" => "menu-icon fa fa-cube",
                );
                if($n['onclick'] == 1){
                    $ns[$i]['onclick'] = $n['href'];
                }else{
                    $ns[$i]["file"]=$n['href'];
                }
                if(isset($n['son'])){
                    foreach($n['son'] as $k=>$v){
                        if(!checkPermissions($v['id'])){
                            continue;
                        }
                        if($v['menu_status'] == 1){
                            $ns[$i]['sub'][$k] = array(
                               "name" => $v['title'],
                               "icon" => "menu-icon fa fa-cube",
                            );
                            if($v['onclick'] == 1){
                                $ns[$i]['sub'][$k]['onclick'] = $v['href'];
                            }else{
                                $ns[$i]['sub'][$k]["file"]=$v['href'];
                            }
                        }else{
                           $ns[$i]['active'][] = $v['href'];
                        }
                        if(isset($v['son']) ){
                            foreach($v['son'] as $no){
                                $ns[$i]['sub'][$k]['active'][] = $no['href'];
                            }
                        }
                        
                    }
                }
                $leftNav['menu'] = $ns;
            }
        }
    }
};
return $leftNav;
echo "<pre>";
print_r($leftNav);exit;


